/**
 * New node file
 */

var jade = require('jade');
var util = require('util');


 exports.Test_showTables = function (req, res) {
    var DBPool = req.app.get('Test_Guild_Master');

    DBPool.getConnection(function (err, conn) {
        if (err) {
            throw err;
        }

        var strQuery = util.format("show Tables;");

        conn.query(strQuery, function (err, rows, fields) {
            conn.release();
            if (err) {
                console.log(err);
            } else {
                res.render('jjaehuny/Test/showTables_guild', { type: "TEST SERVER", server: "TEST SERVER", rows: rows, fields: fields });
            }
        })
    })
};

exports.Test_showTable = function(req, res){

    var tableName = req.param('tableName');

    var DBPool = req.app.get('Test_Guild_Master');
    DBPool.getConnection(function(err, conn) {
        if (err) {
            throw err;
        }

        var strQuery = util.format("select * from %s;", tableName);
        console.log("query : " + strQuery);

        conn.query(strQuery, function (err, rows, fields) {
            if (err) {
                console.log(err);
            } else {
                console.log(rows);
                res.render('jjaehuny/layout', { type: "TEST SERVER", rows: rows, fields: fields });
            }
            conn.release();
        })
    })
};



exports.Real_showTables = function (req, res) {
    var DBPool = req.app.get('Real_Guild_Master');

    DBPool.getConnection(function (err, conn) {
        if (err) {
            throw err;
        }

        var strQuery = util.format("show Tables;");

        conn.query(strQuery, function (err, rows, fields) {
            conn.release();
            if (err) {
                console.log(err);
            } else {
                res.render('jjaehuny/Real/showTables_guild', { type: "REAL SERVER", server: "REAL SERVER", rows: rows, fields: fields });
            }
        })
    })
};


exports.Test_find_guild_info_name = function(req, res) {

    var name = req.param('name');
    console.log(name);

    var DBPool = req.app.get('Test_Guild_Master');

    DBPool.getConnection(function (err, conn) {
        if (err) {
            throw err;
        }

        var strQuery = util.format("select * from guild_info where guild_name = '%s';", name);

        conn.query(strQuery, function (err, rows, fields) {
            conn.release();
            if (err) {
                console.log(err);
            } else {
                res.render('jjaehuny/layout', { type: "TEST GUILD SERVER", rows: rows, fields: fields });
            }
        })
    })
};

exports.Real_showTable = function(req, res){

    var tableName = req.param('tableName');

    var DBPool = req.app.get('Real_Guild_Master');
    DBPool.getConnection(function(err, conn) {
        if (err) {
            throw err;
        }

        var strQuery = util.format("select * from %s;", tableName);
        console.log("query : " + strQuery);

        conn.query(strQuery, function (err, rows, fields) {
            if (err) {
                console.log(err);
            } else {
                console.log(rows);
                res.render('jjaehuny/layout', { type: "REAL SERVER", rows: rows, fields: fields });
            }
            conn.release();
        })
    })
};


exports.Real_find_guild_info_name = function(req, res) {

    var name = req.param('name');
    console.log(name);

    var DBPool = req.app.get('Real_Guild_Master');

    DBPool.getConnection(function (err, conn) {
        if (err) {
            throw err;
        }

        var strQuery = util.format("select * from guild_info where guild_name = '%s';", name);

        conn.query(strQuery, function (err, rows, fields) {
            conn.release();
            if (err) {
                console.log(err);
            } else {
                res.render('jjaehuny/layout', { type: "Real GUILD SERVER", rows: rows, fields: fields });
            }
        })
    })
};
